package com.fcl.mylibrary.recyclerviewhelper;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fcl.mylibrary.R;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/20.
 */

public class BaseAdapter<H> extends RecyclerView.Adapter<ViewHolder> {

    private static final int TYPE_ITEM = 0; //普通item
    private static final int TYPE_FOOTER = 1; //底部view

    private static final int PAGE_SIZE = 20;

    private int mLayoutId;
    protected List<H> mDatas = new ArrayList<>();
    private WeakReference<Context> mContextRef;
    protected Context mContext;
    private FooterHolder mFooterHolder;
    private MyClickListener mMyClickListener;
    private boolean mShowFooter = true;

    public BaseAdapter(Context context, int layoutId, List<H> datas) {
        this.mLayoutId = layoutId;
        this.mDatas = datas;
        mContextRef = new WeakReference<Context>(context);
        mContext = mContextRef.get();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(mContext).inflate(mLayoutId, parent, false);

            BaseHolder holder = new BaseHolder(view);
            return holder;
        } else if (viewType == TYPE_FOOTER){
            View view = LayoutInflater.from(mContext).inflate(R.layout.footer_layout, parent, false);

            mFooterHolder = new FooterHolder(view);
            return mFooterHolder;
        }
        return null;

    }

    @Override
    public int getItemViewType(int position) {
        if (position+1==getItemCount() && mShowFooter) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (holder instanceof BaseHolder) {
            bindHolder((BaseHolder) holder, mDatas.get(position));
            ((BaseHolder)holder).mRootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mMyClickListener != null) {
                        mMyClickListener.onClick(position);
                    }
                }
            });
        }
    }

    protected void bindHolder(BaseHolder baseHolder, H data) {

    }

    @Override
    public int getItemCount() {
        int size = mDatas.size();
//        if (mDatas.size() % PAGE_SIZE == 0) {
//            return size+1;
//        } else {
//            return size;
//        }
        if (mShowFooter) {
            return size+1;
        } else {
            return size;
        }
    }

    /**
     * 设置是否显示底部，默认显示
     * @param mShowFooter
     */
    public void setShowFooter(boolean mShowFooter) {
        this.mShowFooter = mShowFooter;
        notifyDataSetChanged();
    }

    /**
     * 设置新数据
     * @param mDatas
     */
    public void setDatas(List<H> mDatas) {
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }

    /**
     * 添加数据
     * @param datas
     */
    public void addDatas(List<H> datas) {
        this.mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    /**
     * 没有更多数据
     */
    public void noMoreData() {
        mFooterHolder.mProgressBar.setVisibility(View.GONE);
        mFooterHolder.mContent.setText(R.string.nomore_data);
    }

    public void setItemClickListener(MyClickListener mMyClickListener) {
        this.mMyClickListener = mMyClickListener;
    }

    /**
     * 点击回调
     */
    public interface MyClickListener{
        void onClick(int pos);
    }
}
